package com.imay.live.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.imay.base.activity.BaseWebViewActivity;
import com.imay.live.Constants;
import com.imay.live.activity.dynamics.ReleaseActivity;
import com.imay.live.helper.H5Helper;
import com.imay.live.R;

/**
 * Created by jeaman on 2016/10/24.
 */

public class WebViewActivity extends BaseWebViewActivity {
    private final String TAG = getClass().getSimpleName();

    public static void startActivity(Context context, String url) {
        startActivity(context, null, url);
    }

    public static void startActivity(Context context, String title, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra(EXTRA_NAME_TITLE, title);
        intent.putExtra(EXTRA_NAME_URL, url);
        context.startActivity(intent);
    }

    public static void startActivityForResult(Activity activity, String title, String url, int requestCode) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra(EXTRA_NAME_TITLE, title);
        intent.putExtra(EXTRA_NAME_URL, url);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        init(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mWebView = $(R.id.webview_wv_webview);
        mProgressBar = $(R.id.webview_pb_progress);
        init(getIntent());
    }

    @Override
    protected void startSchemeAction(Uri uri) {
//        url = "imay://com.imay.live/openwith?type=3&FeedId=29277&uid=24469";
//        url = "imay://com.imay.live/openwith?type=2&uid=24469";
//        url = "imay://com.imay.live/openwith?type=1&roomId=129703";
//        url = "imay://com.imay.live/openwith?type=1&LabelContent=长腿";
//        url = "imay://com.imay.live/openwith?type=10&equiqId=3008";
//        url = "imay://com.imay.live/openwith?type=8&imageUrl=http://portrait5.sinaimg.cn/1216826604/blog/180&title=11&text=000&url=www.baidu.com";
        try {
            if (H5Helper.isAppSchemeUri(uri)) {
                H5Helper.startActivity(uri.toString(), this);
            } else {
                super.startSchemeAction(uri);
            }
        } catch (Throwable r) {
            r.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Constants.REQUEST_PICK_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    if (data == null) {
                        return;
                    }
                    Bundle b = data.getExtras();
                    String imagePath = b.getString("filePath");
                    ReleaseActivity.startActivity(this, imagePath, null, null, ReleaseActivity.IMAGE, Constants.DYNAMICSFEED_REQUEST_CODE);
                }
                break;
            case Constants.REQUEST_PICK_VIDEO:
                if (resultCode == Activity.RESULT_OK) {
                    if (data == null) {
                        return;
                    }
                    Bundle b = data.getExtras();
                    String movPath = b.getString("filePath");
                    String imgPath = b.getString("imagePath");
                    ReleaseActivity.startActivity(this, imgPath, movPath, null, ReleaseActivity.MOVIE, Constants.DYNAMICSFEED_REQUEST_CODE);
                }
                break;
        }
    }


}
